import type { Router } from 'vue-router';
import { setRouteEmitter } from '@/utils/route-listener';
import setupUserLoginInfoGuard from './userLoginInfo';
import { useAppStore } from '@/store';

function checkPaths(str: any) {
  return (
    str.includes('/files/personal') ||
    str.includes('/files/public') ||
    str.includes('/files/archive')
  );
}
const checkDashboard = (str: string | undefined): boolean => {
  return !!str && checkPaths(str);
};
function setupPageGuard(router: Router) {
  router.beforeEach(async (to) => {
    // emit route change
    setRouteEmitter(to);
    const appStore = useAppStore();
    // 判断需不需要展开子菜单
    if (checkDashboard(to.path)) {
      appStore.toggleMenu2Collapse(false);
    } else {
      appStore.toggleMenu2Collapse(true);
    }
  });
}

export default function createRouteGuard(router: Router) {
  setupPageGuard(router);
  setupUserLoginInfoGuard(router);
}
